﻿#define _CRT_SECURE_NO_WARNINGS 1
#include "SList.h"
//打印节点中的数据
void PrintStudent(StudentNode* phead)
{
    StudentNode* pcur = phead;
    int index = 1;  // 添加序号，便于阅读

    while (pcur != NULL)
    {
        printf("=== 学生 %d ===\n", index);
        printf("学号:     %s\n", pcur->id);
        printf("姓名:     %s\n", pcur->name);
        printf("学院:     %s\n", pcur->college);
        printf("专业:     %s\n", pcur->major);
        printf("班级:     %s\n", pcur->class_name);
        printf("性别:     %s\n", pcur->gender);
        printf("年龄:     %d\n", pcur->age);
        printf("--------------------------\n");
        pcur = pcur->next;
        index++;
    }

    printf("NULL\n");  // 链表结束
}

//头插节点
void SLTPushFrontInteractive(StudentNode** phead) {
    assert(phead);

    char id[IDMAX];
    char name[NAMEMAX];
    char college[COLLEGE];
    char major[MAJOR];
    char class_name[CLASSNAME];
    char gender[GENDER];
    int age;

    printf("\n=== 请输入新学生的信息 ===\n");

    printf("请输入学号: ");
    scanf("%s", id);

    printf("请输入姓名: ");
    scanf("%s", name);

    printf("请输入学院: ");
    scanf("%s", college);

    printf("请输入专业: ");
    scanf("%s", major);

    printf("请输入班级: ");
    scanf("%s", class_name);

    printf("请输入性别 (男/女): ");
    scanf("%s", gender);

    printf("请输入年龄: ");
    while (scanf("%d", &age) != 1) {
        printf("输入错误，请输入一个整数年龄: ");
        while (getchar() != '\n');
        scanf("%d", &age);
    }

    // 创建新节点
    StudentNode* newNode = (StudentNode*)malloc(sizeof(StudentNode));
    if (newNode == NULL) {
        printf(" 内存分配失败！\n");
        return;
    }

    // 填充数据
    strncpy(newNode->id, id, IDMAX - 1);
    newNode->id[IDMAX - 1] = '\0';

    strncpy(newNode->name, name, NAMEMAX - 1);
    newNode->name[NAMEMAX - 1] = '\0';

    strncpy(newNode->college, college, COLLEGE - 1);
    newNode->college[COLLEGE - 1] = '\0';

    strncpy(newNode->major, major, MAJOR - 1);
    newNode->major[MAJOR - 1] = '\0';

    strncpy(newNode->class_name, class_name, CLASSNAME - 1);
    newNode->class_name[CLASSNAME - 1] = '\0';

    strncpy(newNode->gender, gender, GENDER - 1);
    newNode->gender[GENDER - 1] = '\0';

    newNode->age = age;

    // 头插法核心：新节点指向原头
    newNode->next = *phead;
    *phead = newNode;  // 修改外部的 head 指针

    printf(" 学生【%s】已成功添加！\n", name);
}

StudentNode* STLFindByName(StudentNode* head, char name[]) {
    StudentNode* curr = head;
    while (curr != NULL) {
        if (strcmp(curr->name, name) == 0) 
        {
            printf("找到了\n");
            return curr;  // 找到，返回当前节点
        }
        curr = curr->next;
    }
    printf("没找到\n");
    return NULL;  // 遍历结束未找到
}

void STLDeleteNode(StudentNode** phead, StudentNode* nodeToDelete) 
{
    assert(phead && nodeToDelete);
    assert(*phead);

    StudentNode* prev = *phead;
    //首先得先判断要删除的节点是不是头节点，如果是头节点，直接释放就可以
    if (nodeToDelete == *phead)
    {
        //头删
        StudentNode* next = (*phead)->next;
        free(*phead);
        *phead = next;
        return;
    }
    else
    {
        //然后如果不是头节点，则开始找要删除的节点
        while (prev->next != nodeToDelete) {

            prev = prev->next;
        }
        //此时prev是想要删除的前一个节点，而pcur在想要删除的节点位置
        prev->next = prev->next->next;
        free(nodeToDelete);
        nodeToDelete = NULL;
    }
}